package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConst;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.Menu;
import com.itheima.service.MenuService;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * @author mengwei
 * @date 2019/8/22
 * @desc
 */
@RestController
@RequestMapping("/menu")
public class MenuController {

    @Reference
    MenuService menuService;

    @RequestMapping("/getMenuList")
    public Result getMenuList(String username){
        try {
            List<Menu> menuList = menuService.getMenuList(username);
            //定义新的返回的集合
            List<Menu> parentMenuList = new ArrayList<>();
            for (Menu menu : menuList) {
                //获取一级菜单的Id
                Integer id = menu.getId();
                //获取一级菜单包含的二级菜单集合
                List<Menu> children = menu.getChildren();
                for (Menu menu1 : menuList) {
                    //遍历如果一级菜单的Id和二级菜单的ParentMenuId相等，
                    //把menu1添加到外循环定义的二级菜单集合中
                    if(id.equals(menu1.getParentMenuId())){
                        children.add(menu1);
                    }
                }
                //ParentMenuId为null的话，就是一级菜单
                if(menu.getParentMenuId() == null){
                    parentMenuList.add(menu);
                }
            }
            return new Result(true, MessageConst.GET_MENU_SUCCESS,parentMenuList);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConst.GET_MENU_FAIL);
        }
    }

    @RequestMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean){
        return menuService.findPage(queryPageBean);
    }

    @RequestMapping("/findChildListById")
    public Result findChildListById(Integer id){
        try {
            List<Menu> childList = menuService.findChildListById(id);
            return new Result(true,MessageConst.GET_MENU_SUCCESS,childList);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConst.GET_MENU_FAIL);
        }
    }

    @RequestMapping("/add")
    public Result add(@RequestBody Menu menu){
        try {
            menuService.add(menu);
            return new Result(true,MessageConst.ADD_MENU_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(true,MessageConst.ADD_MENU_FAIL);
        }
    }

    @RequestMapping("/addChild")
    public Result addChild(@RequestBody Menu menu){
        try {
            menuService.addChild(menu);
            return new Result(true,MessageConst.ADD_MENU_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(true,MessageConst.ADD_MENU_FAIL);
        }
    }

    @RequestMapping("/update")
    public Result update(@RequestBody Menu menu){
        try {
            menuService.update(menu);
            return new Result(true,MessageConst.EDIT_MENU_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConst.EDIT_MENU_FAIL);
        }
    }

    @RequestMapping("/delById")
    public Result delById(Integer id){
        try {
            menuService.delById(id);
            return new Result(true,MessageConst.DELETE_MENU_SUCCESS);
        } catch (RuntimeException e) {
            e.printStackTrace();
            return new Result(false,e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConst.DELETE_MENU_FAIL);
        }
    }

    @RequestMapping("/findMenuChildById")
    public Result findMenuChildById(Integer id){
        try {
            Menu menu = menuService.findMenuChildById(id);
            return new Result(true,MessageConst.GET_MENU_SUCCESS,menu);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConst.GET_MENU_FAIL);
        }
    }

    @RequestMapping("/editMenuChild")
    public Result editMenuChild(@RequestBody Menu menu){
        try {
            menuService.editMenuChild(menu);
            return new Result(true,MessageConst.EDIT_MENU_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConst.EDIT_MENU_FAIL);
        }
    }

    @RequestMapping("/delChildById")
    public Result delChildById(Integer id){
        try {
            menuService.delChildById(id);
            return new Result(true,MessageConst.DELETE_MENU_SUCCESS);
        } catch (RuntimeException e) {
            e.printStackTrace();
            return new Result(false,e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConst.DELETE_MENU_FAIL);
        }
    }

    @RequestMapping("/findAll")
    public Result findAll(){
        try {
            List<Menu> menuList = menuService.findAll();
            //定义新的返回的集合
            List<Menu> parentMenuList = new ArrayList<>();
            for (Menu menu : menuList) {
                //获取一级菜单的Id
                Integer id = menu.getId();
                //获取一级菜单包含的二级菜单集合
                List<Menu> children = menu.getChildren();
                for (Menu menu1 : menuList) {
                    //遍历如果一级菜单的Id和二级菜单的ParentMenuId相等，
                    //把menu1添加到外循环定义的二级菜单集合中
                    if(id.equals(menu1.getParentMenuId())){
                        children.add(menu1);
                    }
                }
                //ParentMenuId为null的话，就是一级菜单
                if(menu.getParentMenuId() == null){
                    parentMenuList.add(menu);
                }
            }
            return new Result(true,MessageConst.GET_MENU_SUCCESS,parentMenuList);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConst.GET_MENU_FAIL);
        }
    }
}
